package com.guaju.yueshangLife.activity.setting;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.view.View;

import com.guaju.yueshangLife.R;
import com.guaju.yueshangLife.base.BaseActivity;
import com.guaju.yueshangLife.databinding.ActivityEditUsernameBinding;
import com.guaju.yueshangLife.net.ApiEntity;
import com.guaju.yueshangLife.textwatcher.SimpleTextWatcher;
import com.guaju.yueshangLife.utils.ToastUtil;

public class EditUsernameActivity extends BaseActivity {
    public static final String USERNAME = "username";
    private String correctName;
    private String originName;
    private ActivityEditUsernameBinding binding;
    private String username;

    @Override
    protected void loadViewLayout() {
        binding = DataBindingUtil.setContentView(this,R.layout.activity_edit_username);

    }

    @Override
    protected void bindViews() {

    }

    @Override
    protected void processLogic(Bundle savedInstanceState) {
        Intent intent = getIntent();
        username = intent.getStringExtra("username");
        binding.etNewName.setText(username);
    }

    @Override
    protected void setListener() {
        binding.etNewName.addTextChangedListener(new SimpleTextWatcher() {
            @Override
            protected void afterTxtChanged(Editable s) {
                correctName = binding.etNewName.getText().toString().trim();
                if (!TextUtils.isEmpty(correctName)){
                    binding.btSubmit.setBackgroundResource(R.drawable.shape_login_enable_bg);
                    binding.flClear.setVisibility(View.VISIBLE);
                }else{
                    binding.btSubmit.setBackgroundResource(R.drawable.shape_login_unable_bg);
                    binding.flClear.setVisibility(View.INVISIBLE);
                }

            }
        });
        binding.btSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                correctName = binding.etNewName.getText().toString().trim();
                if (!TextUtils.isEmpty(correctName)){
                    if (TextUtils.equals(correctName,username)){
                        ToastUtil.show("与原用户名一致");
                    }else{
//                        提交新用户名
                        ApiEntity.updateMineInfoUsername(correctName);
                        Intent intent = new Intent();
                        Bundle bundle = new Bundle();
                        bundle.putString(USERNAME,correctName);
                        setResult(MyInfoActivity.RESULT_USERNAME,intent);
                        finish();
                    }
                }else{
                    ToastUtil.show("未设置新的用户名");
                }
            }
        });
        binding.flClear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                correctName = binding.etNewName.getText().toString().trim();
                if (!TextUtils.isEmpty(correctName)){
                    binding.etNewName.setText("");
                }
            }
        });


    }
}
